package cn.com.fattiger.framework.cms.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/12/23.
 */

@Controller
public class ApiController {

    @Autowired
    private Dispatcher dispatcher;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(value = "gateway.do")
    @ResponseBody
    public Object gateway(@RequestBody RequestObject request){
        try {
            System.out.println(request);
            //0.logging
            //1.verify
            //2.validate
            //3.overload detect.
            //4.upgrade detect.
            //5.permission detect.
            //6.ip table detect.
            Object response = dispatcher.dispatch(request);
            return response;
        }catch(Exception e){
            HashMap<String, Object> response = new HashMap<String, Object>();
            response.put("code", 500);
            //code:module+type+number
            //load message by code from cache.
            response.put("message", e.getMessage());
            return response;
        }
    }

}
